<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--   获取绝对路径 -->
<c:set var="ctx" value="${pageContext.request.contextPath }" />
<c:set var="d" value="${requestScope.department }" />
<%@ include file="/common/include/include.jsp"%>
</head>
<body>

	<div id="content-container">
		<%@ include file="/common/include/pageTitleBread.jsp"%>
	<div id="page-content">
			<div class="col-lg-12">
		        <div class="panel">
		            <div class="panel-heading">
		                <h3 class="panel-title">${requestScope.nowBread }</h3>
		            </div>
		            

		            
		            <form id="editForm" class="panel-body form-horizontal form-padding">
		            
		            	<input type="hidden" id ="pId" name="parentdepartmentid"  value="${d!=null?d.parentdepartmentid:'0' }">
						<input type="hidden" id = "orgId" name="id" value="${d!=null?d.id:'' }">
				    	<input type="hidden" id = "opreate" name="opreate" value="${requestScope.nowBread }">
		            
		            	<c:if test="${requestScope.nowBread!='修改机构'}">
			            	<div class="form-group">
			                    <label class="col-md-3 control-label" for="demo-text-input">父级机构</label>
			                    <div class="col-md-5">
			                        <div class="input-group mar-btm">
				                        <input type="text" id="pName" value="${d!=null?d.parent_name:'根目录' }" readonly class="form-control">
										
				                        
				                        
				                        <span class="input-group-btn">
				                            <button class="btn btn-mint" type="button" onclick="selectOneOrg()">选择</button>
				                        </span>
				                    </div>
			                        <small class="help-block">请选择</small>
			                    </div>
			                </div>
		                </c:if>
		                <div class="form-group">
		                    <label class="col-md-3 control-label" for="demo-text-input">机构名称</label>
		                    <div class="col-md-9">
		                        <input type="text"  name="name" class="form-control"  value="${d!=null?d.name:'' }">
		                        <small class="help-block">请输入机构名称</small>
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-md-3 control-label" for="demo-textarea-input">描述信息</label>
		                    <div class="col-md-9">
		                        <textarea name="description" rows="9" class="form-control">${d!=null?d.description:'' }</textarea>
		                        <small class="help-block">请输入机构的描述信息</small>
		                    </div>
		                </div>
		               <c:if test="${requestScope.nowBread!='修改机构'}">
			                <div class="form-group">
			                    <label class="col-md-3 control-label" for="demo-text-input">显示顺序</label>
			                    <div class="col-md-4">
			                        <input type="text" id="sort" name="sort" class="form-control" value="${d!=null?d.sort:'' }">
			                        <small class="help-block">请输入机构显示顺序</small>
			                    </div>
			                </div>
		                </c:if>
		                <div class="panel-footer">
		                    <div class="row">
		                        <div class="col-sm-9 col-sm-offset-3">
		                            <button class="btn btn-warning" type="reset">重置</button>
		                            <button class="btn btn-mint" type="submit" >保存</button>
		                        </div>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>
	</div>
	
	
	<!-- 执行保存 -->
	<script>
		function save(){
			var opreate=$("#opreate").val();//进行的操作
			if(opreate!="修改机构"){
				var sort=$("#sort").val();//显示顺序
				var pId=$("#pId").val();//父机构id
				//新增部门id为      父机构id拼接显示顺序
				$("#orgId").val(pId+""+sort);	
			}

			var data = common_ajax.ajaxFunc("/admin/department/saveOrUpdate", $('#editForm').serialize(), "json", null);
			
			if(data.success){
				pointLion.alertMsg("保存成功!" , "success" , "small" , function(){
					doPjax(ctx+'/admin/department/getListPage');//跳转到列表页
				});
			}
		}
		
		
	</script>
	
	
	
	<!-- 提供一个弹出的机构树形图给用户选择父机构 -->
	<script>
		function selectOneOrg(){
			//调用选择一个单位组件
			pointLion.selectOneOrg(function(orgNode){
				var thisId = $("#orgId").val();//主键
				if(thisId==orgNode.id){
					 layer.closeAll();
					 pointLion.alertMsg("您不能选择自己为父级节点!" , "warning" , "small" , null);
					 return;
				}
				$("#pName").val(orgNode.name);//所选单位名称
				$("#pId").val(orgNode.id);//所选单位id
			});
		}
	</script>
	
	
	<!-- 数据校验，通过则执行保存	 -->
	<script>
		$(document).ready(function() {
		    	$('#editForm').bootstrapValidator({
		            fields: {
		            	"name": {
		                    validators: {
		                        notEmpty: {
		                            message: '*机构名称不能为空'
		                        },
		                        stringLength: {/*长度提示*/
		                            max: 25,
		                            message: '*机构名称长度必须小于25'
		                        }
		                    }
		                },
		                "sort": {
		                    validators: {
		                    	lessThan: {
		        					inclusive:true,
		        					value: 20,
		        					message: '*请输入小于20的数字'
		        				}
		                    }
		                },
		                "description": {
		                    validators: {
		                    	stringLength: {
		                            max: 2000,
		                            message: '*机构描述信息长度必须小于2000'
		                        }
		                    }
		                }
		            }
		        }).on("success.form.bv", function (e) {
		            save();
		            return false;//阻止表单跳转
		        });
		});
	</script>
</div>
</body>
</html>
	